import create from 'zustand';
import { AuthUser, UserRole } from '../types/roles';

interface NotificationItem {
  id: string;
  content: string;
  read: boolean;
  type: 'info'|'warning'|'success'|'error';
}

interface GlobalState {
  user: AuthUser | null;
  setUser: (u: AuthUser|null) => void;
  loading: boolean;
  setLoading: (loading: boolean) => void;
  notifications: NotificationItem[];
  addNotification: (n: Omit<NotificationItem,'id'>) => void;
  markRead: (id: string) => void;
  clearNotifications: () => void;
  // 患者端相关状态
  patient: {
    appointments: any[];
    emrs: any[];
    setAppointments: (a: any[]) => void;
    setEMRs: (e: any[]) => void;
  };
  // 医护端相关状态
  doctor: {
    todoList: any[];
    patientList: any[];
    setTodoList: (t: any[])=>void;
    setPatientList: (p: any[])=>void;
  };
}

export const useGlobalStore = create<GlobalState>((set) => ({
  user: null,
  setUser: (u) => set({ user: u }),
  loading: false,
  setLoading: (loading) => set({ loading }),
  notifications: [],
  addNotification: (n) => set((state)=>({ notifications: [...state.notifications, { id: Date.now()+Math.random()+'', ...n, read: false }] })),
  markRead: (id) => set((state)=>({ notifications: state.notifications.map(notif=>notif.id===id?{...notif,read:true}:notif) })),
  clearNotifications: () => set({notifications: []}),
  patient: {
    appointments: [],
    emrs: [],
    setAppointments: (a) => set(state=>({ patient: { ...state.patient, appointments: a }})),
    setEMRs: (e) => set(state=>({ patient: { ...state.patient, emrs: e }}))
  },
  doctor: {
    todoList: [],
    patientList: [],
    setTodoList: (t)=>set(state=>({ doctor: { ...state.doctor, todoList: t }})),
    setPatientList: (p)=>set(state=>({ doctor: { ...state.doctor, patientList: p }}))
  }
}));
